package com.yazi.mxz.common.service.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.myframework.smhj.common.tool.DateUtil;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.dao.BasicService;
import com.myframework.smhj.hibernate.dao.Dao;
import com.yazi.mxz.common.client.ActFactory;
import com.yazi.mxz.common.db.dao.SmsDao;
import com.yazi.mxz.common.entity.ValueFactory;
import com.yazi.mxz.common.entity.beans.Sms;
import com.yazi.mxz.common.service.SmsService;

@Service("smsService")
public class SmsServiceImpl extends BasicService<Sms> implements SmsService {
	private static Logger log = LoggerFactory.getLogger(SmsServiceImpl.class);
	
	@Resource
	SmsDao smsDao;
	
	@Override
	protected Dao<Sms> getDao() {
		return smsDao;
	}
	
	@Override
	public Sms add(Sms sms) {
		log.debug("add: toMobile={},type={}", sms.getToMobile(), sms.getType());
		
		ValueFactory.validate(sms);
		sms.setSmsId(null);
		sms.setAddTime(DateUtil.getTimeStamp());
		sms = smsDao.persist(sms);
		
		// 触发短信发送
		ActFactory.actSmsJob();
		
		return sms;
	}
	
	@Override
	public Page<Sms> listByGtAddtime(Sms sms, Searching si) {
		log.debug("listByGtAddtime：{}", DateUtil.getDateTimeStr(sms.getAddTime()));
		return smsDao.listByGtAddtime(sms, si);
	}

	@Override
	public Sms edit(Sms sms) {
		log.debug("edit: smsId={}", sms.getSmsId());
		Sms dstSms = smsDao.get(sms.getSmsId());
		ValueFactory.combine(sms, dstSms);
		return smsDao.merge(sms);
	}
	
}
